package com.component.api.model.dto;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.component.api.function.ConversionFunction;
import com.common.utils.tool.StringUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * 描述：
 * 字典规则
 * @author xianggj
 * @Date 2021/11/29 14:05
 **/
@Data
@NoArgsConstructor
public class RuleColumnDTO extends ColumnDTO implements Serializable {
    private static final long serialVersionUID = -1L;


    /**
     * 主值
     */
    private String mainVal;

    /**
     * 同义词映射表
     * 副值：主值
     */
    private List<String> synonyms;

    /**
     * 消歧
     */
    private Rule disambiguation;

    /**
     * 消歧规则
     */
    @Data
    @NoArgsConstructor
    public static class Rule implements Serializable {

        private static final long serialVersionUID = -1L;

        /**
         * 相识度
         */
        private Double similarity;

        /**
         * 前N个
         */
        private Integer startWith;

        /**
         * 后N个
         */
        private Integer endWith;
    }

    /**
     * 获取计算公式
     * @return
     */
    public ConversionFunction getFunction(){
        if (CollUtil.isEmpty(synonyms) || StrUtil.isBlank(mainVal)) {
            return a -> a;
        } else {
            return a-> {
                if (CollUtil.contains(synonyms, a)){
                    return mainVal;
                }
                //这里需要同时满足条件
                boolean flag1 = true;
                boolean flag2 = true;
                boolean flag3 = true;
                if (disambiguation != null){
                    if (disambiguation.similarity != null){
                        flag1 = StrUtil.similar(a, mainVal) > disambiguation.similarity ;
                    }
                    if (disambiguation.startWith != null ){
                        flag2 = StringUtil.startWithToN(a, mainVal, disambiguation.startWith);
                    }
                    if (disambiguation.endWith != null){
                        flag3 = StringUtil.endWithToN(a, mainVal, disambiguation.endWith);
                    }
                }
                if (flag1 && flag2 && flag3){
                    a = mainVal;
                }
                return a;
            };
        }
    }
}
